package kr.inani.android.view;

import kr.inani.android.search.BookShelf;
import kr.inani.android.view.drag.BookDragInfo;
import kr.inani.android.view.drag.DragSource;
import kr.inani.android.view.drag.DragView;
import kr.inani.android.view.drag.DropTarget;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class CustomLinearLayout extends LinearLayout implements DropTarget 
{
	private BookShelf bookShelf;
	
	private Rect preRect = new Rect();
	private Rect nextRect = new Rect();
	private Point prePoint = new Point();
	private Point nextPoint = new Point();
	
	public CustomLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CustomLinearLayout(Context context) {
		super(context);
	}

	public void setBookShelf(BookShelf bookShelf)
	{
		this.bookShelf = bookShelf;
	}
	
	////////////////////// dropTarget////////////////////////
	@Override
	public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, DragView dragView, Object dragInfo) {
		BookDragInfo bookDragInfo = (BookDragInfo) dragInfo;
		int scrolledX = ((BookView) getChildAt(0)).getScrolledX();
		for(int i=1;i<getChildCount();i++) {
			BookView preBookView = (BookView) getChildAt(i);
			boolean isPreVisible = preBookView.getGlobalVisibleRect(preRect, prePoint); 
			if(!isPreVisible) {
				continue ;
			}
			BookView nextBookView = (BookView) getChildAt(i+1);
			if(nextBookView!=null) {
				boolean isNextVisible = nextBookView.getGlobalVisibleRect(nextRect, nextPoint);
				if(!isNextVisible) {
					bookShelf.insertBook(bookDragInfo.getBookItem(), i);
					return ;
				}
				if(i==1 && x<(preRect.left+scrolledX)) {
					bookShelf.insertBook(bookDragInfo.getBookItem(), 0);
					return ;
				} else if(x>=(preRect.left+scrolledX) && x<=(nextRect.right+scrolledX)) {
					bookShelf.insertBook(bookDragInfo.getBookItem(), i);
					return ;
				}
				
			} else {
				bookShelf.insertBook(bookDragInfo.getBookItem(), i);
			}
		}
		
	}

	@Override
	public void onDragEnter(DragSource source, int x, int y, int xOffset,
			int yOffset, DragView dragView, Object dragInfo) {
		
	}

	@Override
	public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, DragView dragView, Object dragInfo) {
//		Log.d("inani", "x="+x+",y="+y+",xOffset="+xOffset+",yOffset="+yOffset);
	}

	@Override
	public void onDragExit(DragSource source, int x, int y, int xOffset,
			int yOffset, DragView dragView, Object dragInfo) {
		
	}

	@Override
	public boolean acceptDrop(DragSource source, int x, int y, int xOffset,
			int yOffset, DragView dragView, Object dragInfo) {
		return true;
	}

	@Override
	public Rect estimateDropLocation(DragSource source, int x, int y,
			int xOffset, int yOffset, DragView dragView, Object dragInfo,
			Rect recycle) {
		return null;
	}

	///////////// /////////////////////////////
}
